import { getReplaceCustomerList } from '@/api/order'
import { ref } from 'vue'

export const useGetReplaceCustomers = () => {
  const getReplaceCustomersLoading = ref(false)
  const getReplaceCustomersKeyword = ref('')
  const replaceCustomers = ref<Array<OrderDetailAPIResponse.ReplaceCustomer>>([])

  const getReplaceCustomers = async () => {
    if (!getReplaceCustomersKeyword.value) {
      return
    }

    try {
      getReplaceCustomersLoading.value = true
      const res = await getReplaceCustomerList(getReplaceCustomersKeyword.value)
      getReplaceCustomersLoading.value = false
      if (res.data.code === 20000) {
        replaceCustomers.value = res.data.data.row
      }
      return res.data
    } catch (error) {
      getReplaceCustomersLoading.value = false
      return {
        data: {
          row: {},
        },
        code: 10000,
        message: '获取失败，请稍后重试',
        hasOtherCode: false,
        success: false,
        otherCodePrefix: null,
      }
    }
  }

  return {
    replaceCustomers,
    getReplaceCustomersKeyword,
    getReplaceCustomersLoading,
    getReplaceCustomers,
  }
}
